﻿package beautifulWorld2D.engine 
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import beautifulWorld2D.engine.events.EngineEvent;
	import fwk.timer.TimerManager;
	
	public class Engine extends EventDispatcher
	{
		private static var _instance:Engine;
		
		/*private static var _timerGlobal:Timer;
		private static var _timerAnim:Timer;
		private static var _timerRepeat:int; //ms
		
		private static var _tabFuncGlobal:Array;
		private static var _tabFuncAnim:Array;*/
		
		private static var _timerOpti:TimerManager;
		
		public static function get timerRepeat():int
		{
			return _timerOpti.fps;
		}
		
		
		public function Engine($stage:Stage) 
		{
			trace("--> new Engine");
			
			_instance = this;
			
			_timerOpti = new TimerManager(60, $stage);
			
			_timerOpti.type = TimerManager.TIMER_TYPE_SETINTERVAL;
			
			/*_timerRepeat = 1000 / 60;

			
			_tabFuncGlobal = new Array();
			_tabFuncAnim = new Array();
			
			
			_timerGlobal 	= new Timer(_timerRepeat, 0);
			_timerAnim 		= new Timer(_timerRepeat, 0);
			
			if(_timerGlobal != null) _timerGlobal.removeEventListener(TimerEvent.TIMER, onTimerGlobal);
			if(_timerAnim != null) _timerAnim.removeEventListener(TimerEvent.TIMER, onTimerAnim);
			
			_timerGlobal.addEventListener(TimerEvent.TIMER, onTimerGlobal);
			_timerAnim.addEventListener(TimerEvent.TIMER, onTimerAnim);*/
			
			
			
			
			
		}
		/*private static function onTimerGlobal(evt:TimerEvent):void
		{
			
			_instance.dispatchEvent(new EngineEvent(EngineEvent.TIMER_GLOBAL));
			
		}
		private static function onTimerAnim(evt:TimerEvent):void
		{
			_instance.dispatchEvent(new EngineEvent(EngineEvent.TIMER_ANIM));
		}*/
		
		public static function start():void
		{
			//_timerGlobal.start();
			//_timerAnim.start();
			_timerOpti.start();
		}
		public static function stop():void
		{
			//_timerGlobal.stop();
			//_timerAnim.stop();
			_timerOpti.stop();
		}
		public static function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			//if(_instance)
			//_instance.addEventListener(type, listener, useCapture, priority, useWeakReference);
			
			_timerOpti.addEventListener(Event.CHANGE, listener);
			

		}
		public static function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			//if(_instance)
			//_instance.removeEventListener(type, listener, useCapture);
			_timerOpti.removeEventListener(Event.CHANGE, listener);
		}

	}
	
}